package com.fenghongzhang.module_main.net

import com.fenghongzhang.module_main.api.API
import com.fenghongzhang.module_main.api.ApiService
import com.fenghongzhang.module_main.api.MallApiService

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
import javax.inject.Singleton

/**
 * 提供网络模块注入
 */
@InstallIn(SingletonComponent::class)
@Module
object NetModule {
    private const val DEFAULT_TIME_OUT = 60L
   // var cache: Cache = Cache(File("${appContext.externalCacheDir?.path}/okhttp_cache/"), 100 * 1024 * 1024)

    @Singleton
    @Provides
    @CommonRetrofit
    fun provideRetrofit(@CommonOkhttp okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("https://iovapicyclone.zonsenmotor.com/")
            .addConverterFactory(GsonConverterFactory.create())
            //2.6.0以上默认支持协程
            // .addCallAdapterFactory(CoroutinesCallAdapterFactory.invoke())
            .client(okHttpClient)
            .build()
    }

    @Singleton
    @Provides
    @CommonOkhttp
    fun provideOkhttpClient(): OkHttpClient {
        val httpLoggingInterceptor =
            okhttp3.logging.HttpLoggingInterceptor()
            //HttpLoggingInterceptor()
        //if (BuildConfig.DEBUG) {
            httpLoggingInterceptor.level = okhttp3.logging.HttpLoggingInterceptor.Level.BODY
       // }
        return OkHttpClient.Builder()
            .writeTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            .readTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            .connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
           // .cache(cache)
            .addInterceptor(httpLoggingInterceptor)
         //   .addInterceptor(HttpHeaderInterceptor())
//            .sslSocketFactory(
//                SSLSocketFactoryUtil.createSSLSocketFactory(),
//                SSLSocketFactoryUtil.createTrustAllManager()
//            )
         //   .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())
           // .protocols(listOf(Protocol.HTTP_1_1))

            .build()
    }


    @Singleton
    @Provides
    fun provideApiService(@CommonRetrofit retrofit: Retrofit): ApiService {
        return retrofit.create(ApiService::class.java)
    }


    /********************OpenApi*****************************/


    @Singleton
    @Provides
    @OpenApiRetrofit
    fun provideOpenApiRetrofit(@OpenApiOkhttp okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("http://test-zsjc-openapi.qimoyun.com/")
            .addConverterFactory(GsonConverterFactory.create())
            //2.6.0以上默认支持协程
            // .addCallAdapterFactory(CoroutinesCallAdapterFactory.invoke())
            .client(okHttpClient)
            .build()
    }


    @Singleton
    @Provides
    @OpenApiOkhttp
    fun provideOpenApiClient(): OkHttpClient {
        val httpLoggingInterceptor =
            okhttp3.logging.HttpLoggingInterceptor()
       // if (BuildConfig.DEBUG) {
            httpLoggingInterceptor.level =  okhttp3.logging.HttpLoggingInterceptor.Level.BODY
     //   }
        return OkHttpClient.Builder()
            .writeTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            .readTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            .connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
//            .cache(cache)
//            .addInterceptor(httpLoggingInterceptor)
//            .addInterceptor(OpenApiHeaderInterceptor())
//            .sslSocketFactory(
//                SSLSocketFactoryUtil.createSSLSocketFactory(),
//                SSLSocketFactoryUtil.createTrustAllManager()
//            )
           // .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())

            .build()
    }
//
//    //提供OpenApi的请求
    @Singleton
    @Provides
    fun provideMallApiService(@OpenApiRetrofit retrofit: Retrofit): MallApiService {
        return retrofit.create(MallApiService::class.java)
    }


    /**使用相应的限定符为字段或参数添加注解来注入所需的特定类型*/

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonRetrofit

    //对接数引网Retrofit
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class OpenApiRetrofit


    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonOkhttp

    //对接数引网okhttp
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class OpenApiOkhttp

}